#include "stdafx.h"
#include "./glCanvas.h"
#include "./glApp.h"
#include "./glPict.h"
#include "./glTrace.h"

namespace GLES
{

    Bool Canvas::attachPict( Pict* pict )
	{
		do 
		{
			_pict = pict;

			glBindTexture(GL_TEXTURE_2D, pict->getTexture());

			E_Break_If(!eglMakeCurrent(GLES::getContext()->m_eglDisplay, _surf, _surf, GLES::getContext()->m_eglContext));

			E_Break_If(!eglBindTexImage(GLES::getContext()->m_eglDisplay, _surf, EGL_BACK_BUFFER));


			return true;
		} while (0);

		return false;
    }

    Pict* Canvas::detachPict()
    {
		_pict = NULL;
        return NULL;
    }

	GLES::Void Canvas::releaseSurface()
	{
		SAFE_DELETE(_pict);
		if(_surf != EGL_NO_SURFACE)
			eglDestroySurface(eglGetCurrentDisplay(), _surf);
	}

	GLES::Bool Canvas::createSurface( SizeI& size,Uns32 format )
	{

		EGLConfig EglConfig = 0;
		EGLint i32ConfigID;
		EGLint i32BufferSize;
		EGLint i32SampleBuffers;
		EGLint i32Samples;

		EGLDisplay eglDisplay = GLES::getContext()->m_eglDisplay;
		eglQueryContext(eglDisplay, eglGetCurrentContext(), EGL_CONFIG_ID, &i32ConfigID);

		eglGetConfigAttrib(eglDisplay, (EGLConfig) (size_t) i32ConfigID, EGL_BUFFER_SIZE,&i32BufferSize);
		eglGetConfigAttrib(eglDisplay, (EGLConfig) (size_t) i32ConfigID, EGL_SAMPLE_BUFFERS,&i32SampleBuffers);
		eglGetConfigAttrib(eglDisplay, (EGLConfig) (size_t) i32ConfigID, EGL_SAMPLES,&i32Samples);

		    EGLint i32ConfigNo;

		// Setup the configuration list for our surface.
		EGLint conflist[] =
		{
			EGL_CONFIG_CAVEAT, EGL_NONE,
			/*
				Tell it the minimum size we want for our colour buffer, depth size and
				anti-aliasing settings so eglChooseConfig will choose a config that is
				a good match for our window context so we only need a single context.
			*/
			EGL_BUFFER_SIZE, i32BufferSize,
			EGL_DEPTH_SIZE, 16,

			EGL_SAMPLE_BUFFERS, i32SampleBuffers,
			EGL_SAMPLES, i32Samples,

			// The PBuffer bit is the important part as it shows we want a PBuffer
			EGL_SURFACE_TYPE, EGL_PBUFFER_BIT ,
			EGL_BIND_TO_TEXTURE_RGBA, EGL_TRUE,
			EGL_NONE
		};

		// Find and return the config
		E_Return(eglChooseConfig(eglDisplay, conflist, &EglConfig, 1, &i32ConfigNo));

		EGLint list[]=
		{
			// Specify the size of the surface.
			EGL_WIDTH, size.w,
			EGL_HEIGHT, size.h,
			// Target for the texture to store in the pbuffer.
			EGL_TEXTURE_TARGET, EGL_TEXTURE_2D,
			// The format of the texture that will be created when the pBuffer is bound to a texture.
			EGL_TEXTURE_FORMAT, EGL_TEXTURE_RGBA,

			// Signal the end.
			EGL_NONE
		};
		_surf = eglCreatePbufferSurface(eglDisplay, EglConfig, list);
		E_Return(_surf != EGL_NO_SURFACE);

		_pict = new Pict();
		_pict->setDesiredSize(size);
		Return(_pict->createFromBuffer(size,format));		
	}

	GLES::Bool Canvas::createWinSurface(HWND hwnd)
	{
		EGLDisplay eglDisplay = GLES::getContext()->m_eglDisplay;
		_surf = eglCreateWindowSurface(eglDisplay, GLES::getContext()->m_eglContext, hwnd, NULL);
		Return(_surf != EGL_NO_SURFACE);
	}
}

